// pages/one/answer.js
const db = wx.cloud.database()
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    openid:'',
    question_id: '',
    question: null
  },
  choose: function(e){
    var that = this
    var option = e.currentTarget.dataset.option
    var question = that.data.question
    
    if(question.type == '1'){
      question.choosed = option
    }else{
      if(!question.answered){
        var choosed =  question.choosed || []
        var index = choosed.indexOf(option)
        if(index == -1){
          choosed.push(option) 
        }else{
          choosed.splice(index,1)
        }
        question.choosed = choosed.sort()
        
      }
      
    }
    console.log(question.choosed)
    that.setData({
      question
    })
    
  },
  submitTest: function(){
    wx.showLoading({
      title: '加载中',
    })
    var that = this
    var question = that.data.question

    //选择错误 => 记录错题本
    if(JSON.stringify(question.choosed) != JSON.stringify(question.answer)){
      console.log('正确答案',question.answer)
      //检查是否已经插入过
      db.collection('userWrong').where({
        _openid: that.data.openid,
        'question._id':question._id
      }).get({
        success: res => {
          console.log(res.data)
          if(!res.data.length){
            db.collection('userWrong').add({
              data:{
                question
              },
              success: res => {
                console.log(res._id)
              },
              fail: err => {
                console.error('新增失败：', err)
              }
            })
          }else{
            //修改答案
            db.collection('userWrong').doc(res.data[0]._id).update({
              data:{
                'question.choosed': question.choosed
              }
            })
          }
          wx.hideLoading()
          wx.showModal({
            title:'回答错误',
            cancelText: '取消',
            confirmText: '查看解析',
            success (res) {
              if (res.confirm) {
                wx.redirectTo({
                  url: '../one/parse?_id='+question._id+'&choosed='+question.choosed,
                })
              } else if (res.cancel) {
                wx.redirectTo({
                  url: '../index/index',
                })
              }
            }
          })
        },
        fail: err => {
          console.error('查询失败：', err)
        }
      })
      
    }else{
      //选择正确 => 删除错题本的题目
      console.log(that.data.openid,question._id)
      db.collection('userWrong').where({
        _openid: that.data.openid,
        'question._id': question._id
      }).remove()
      wx.hideLoading()
      wx.showModal({
        title: '回答正确',
        showCancel: false,
        success (res) {
          if (res.confirm) {
            wx.redirectTo({
              url: '../index/index',
            })
          } else if (res.cancel) {
            
          }
        }

      })

    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    var question_id = options._id
    var openid = wx.getStorageSync('openid')
    if(!openid){
      wx.setStorageSync('returnUrl', '/pages/one/answer?_id='+question_id)
      wx.navigateTo({
        url: '../index/welcome',
      })
      return false
    }
    that.setData({
      openid,
      question_id
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    wx.showLoading({
      title: '加载中',
    })
    //查询questionBank表数据
    var question_id = this.data.question_id
    db.collection('questionBank').doc(question_id).get({
      success: res => {
        this.setData({
          question: res.data
        })
        wx.setNavigationBarTitle({
          title: res.data.question
        })
        console.log(res.data)
        wx.hideLoading()
      },
      fail: err => {
        console.error('error', err)
      }
    })

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: this.data.question.question,
      path: 'pages/one/answer?_id='+this.data.question_id
    }
  }
})